package com.devframe.sysbase.loader;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.AbstractApplicationContext;

import com.devframe.sys.loader.AbstractSysLoader;
import com.devframe.sysconfig.service.CodeService;
import com.devframe.sysconfig.service.CtrlTableService;
import com.devframe.sysconfig.service.SysParamService;
import com.devframe.sysconfig.storage.operate.ReaderCacheData;
/**
 * 
 * @author 詹保山(softzbs@126.com)
 * @desc: 获取上下文信息的类
 * Jul 9, 2010
 */
public class SysBaseContainer extends AbstractSysLoader{
	private static Logger logger = Logger.getLogger(SysBaseContainer.class);
	private static ContextHelper helper = new ContextHelper();
	private static SysBaseContainer instance;
	
	private static ReaderCacheData  readerCacheData;
	
	private SysBaseContainer() {
		
	}
	private static class ContextHelper  {
		public Object getBean(String beanName) {
			return SysBaseContainer.instance.applicationContext.getBean(beanName);
		}
	}
	private static SysBaseContainer factoryMethod() {
		logger.info("初始化base dev 上下文");
		instance = new SysBaseContainer();
		return instance;
	}
	/**
	 * 
	 * Aug 28, 2011<br/>
	 * @描述：获取当前实例
	 * <br/> 
	 * @return
	 */
	public static SysBaseContainer getInstance(){
		return SysBaseContainer.instance;
	}

	public void init(){
		 logger.info("开始系统初始化...\n");
	        // 启动系统基本服务
	        sysInit();
	        // 启动其他相关服务
	        otherInit();
	        logger.info("系统初始化完毕。\n");
	    }

	    /**
	     * 初始化系统信息，包括系统参数、代码表及控制表加载的操作。
	     * 
	     */
	    protected void sysInit() {
	        // 系统参数初始化
	        logger.info("系统参数初始化");
	        SysParamService sysParamService = (SysParamService) SysBaseContainer.getInstance().getBean("sysParamService");
	        sysParamService.init();
	        //设置系统绝对路径为系统参数加到系统参数缓存中

	        // 表码表初始化(通过系统参数开关决定是否预加载)
	        //if (sysParamService.getBoolSysParam("INIT_LOADING_CODE_SWITCH")) {
	        logger.info("代码表初始化");
	        CodeService codeService = (CodeService) SysBaseContainer.getInstance().getBean("codeService");
	        codeService.init();

	        // 控制表初始化
	        logger.info("控制表初始化");
	        CtrlTableService ctrlTableService = (CtrlTableService) SysBaseContainer.getInstance().getBean("ctrlTableService");
	        ctrlTableService.init();
	        
	        
	    }

	    /**
	     * 定义了一个空操作的方法，继承的类覆盖该方法，完成其他的初始化操作。
	     */
	    protected void otherInit() {
	    	logger.info("设置读取缓存数据句柄");
	    	SysBaseContainer.readerCacheData=(ReaderCacheData)this.applicationContext.getBean("readerCacheData");
	    	
	    }
	
	/**
	 * 根据BeanName的名称获得 BEAN实体
	 * @param beanName bean的名称
	 * @return 
	 * @author:詹保山softzbs@126.com
	 */
	public Object getBean(String beanName) {
		return applicationContext.getBean(beanName);
	}

	/**
	 * reload
	 * 重新初始化spring上下文
	 */
	public void reload() {
	    ((AbstractApplicationContext) applicationContext).close();
		((AbstractApplicationContext) applicationContext).refresh();
	}

	/**
	 * 自动装配一个Bean对象
	 * autowireComponent
	 * @param bean
	 *            bean
	 */
	public void autowireComponent(Object bean) {
		((AbstractApplicationContext) applicationContext).getBeanFactory().autowireBeanProperties(bean,
				AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
	}
	/**
	 * 关闭Spring上下文
	 * close
	 * 
	 */
	public void close() {
		((AbstractApplicationContext) applicationContext).close();
		applicationContext = null;
	}
	
	
	/**
	 * 获取读取缓存数据的句柄
	 * @return
	 */
	public static ReaderCacheData accessCacheDataHandle(){
		return SysBaseContainer.readerCacheData;
	}
}
